﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.Catalog;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Common.Utils;
using Telerik.WebControls;
using PowerCreator.LMS.Paper.Entiry;

public partial class sysadmin_DL_Project_AddClassExam : PowerCreator.LMS.Web.Base.WebBasePage
{
    protected string ClassName = string.Empty;
    private VOTOClass voClass = new VOTOClass();
    TOClass objClass = new TOClass();
    VOTOClassExam voClassExam = new VOTOClassExam();
    TOClassExam objClassExam = new TOClassExam();
    Course opCourse = new Course();
    PowerCreator.LMS.Web.Paper.ManagerPaper opPaper = new PowerCreator.LMS.Web.Paper.ManagerPaper();

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddJob))
        //{
        //    Script.RedirectURL(GetRefUrl());
        //}
        if (!IsPostBack)
        {
            InitPage();

            BinderExam();
            int CID = 0;
            if (Request.QueryString["CID"] != null)
            {
                CID = int.Parse(Request.QueryString["CID"].ToString());
                ViewState["CID"] = CID.ToString();
                voClass = objClass.Load(CID);
                ClassName = voClass.Name;
                BinderCourse(voClass);
            }
            int CourseID = 0;
            if (Request.QueryString["CourseID"] != null)
            {
                CourseID = int.Parse(Request.QueryString["CourseID"].ToString());
                ViewState["CourseID"] = CourseID.ToString();
                ddl_CourseID.SelectedValue = CourseID.ToString();

            }

            int CEID = 0;
            if (Request.QueryString["CEID"] != null)
            {
                CEID = int.Parse(Request.QueryString["CEID"].ToString());
                ViewState["CEID"] = CEID.ToString();
                BinderForm(CEID);
            }


        }
    }
    protected override void InitPage()
    {
        //lbl_InfoTitle.Text = GetLanageValueByKey("lbl_AddJob_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_JobName.Text = GetLanageValueByKey("lbl_AddJob_JobName", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_JobDescription.Text = GetLanageValueByKey("lbl_AddJob_JobDescription", PowerCreator.LMS.Language.LanagetItemType.Page);

        //btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        //btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        //base.InitPage();

        //lbl_InfoTitle_AddNewType.Text = GetLanageValueByKey("lab_AddNewType_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_NewTypeName.Text = GetLanageValueByKey("lab_AddNewType_TypeName", PowerCreator.LMS.Language.LanagetItemType.Page);

        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }


    //绑定课程
    private void BinderCourse(VOTOClass vo)
    {
        VOTOCourseList voCourselist = new VOTOCourseList();
        TOCourseList objCourselist = new TOCourseList();
        voCourselist = objCourselist.Load(vo.ClassCourseListID);
        string CourseIDsList = voCourselist.CourseID;
        string[] courseid = CourseIDsList.Split('#');
        string courseidlist = string.Empty;
        for (int i = 0; i < courseid.Length; i++)
        {
            string[] par = courseid[i].Split(',');
            if (par.Length == 4)
            {
                if (courseidlist.Length > 0)
                    courseidlist += ",";
                courseidlist += par[0];
            }
        }

        if (courseidlist.Length == 0)
            courseidlist = "0";

        string search = " Course_Id in (" + courseidlist + ")";
        DataTable dt = new DataTable();
        dt = opCourse.Query(search);

        ddl_CourseID.DataSource = dt;
        ddl_CourseID.DataTextField = "CourseName";
        ddl_CourseID.DataValueField = "Course_Id";
        ddl_CourseID.DataBind();

        ListItem li = new ListItem();
        li.Text = "综合";
        li.Value = "0";
        ddl_CourseID.Items.Insert(0, li);

    }
    //绑定附件
    private void BinderExam()
    {
        DataTable dt = new DataTable();
        dt =opPaper.QueryPaper(" paperType=0 and paperBuildState=1");

        if (dt.Rows.Count > 0)
        {
            ddl_exam.DataSource = dt;
            ddl_exam.DataTextField = "PaperName";
            ddl_exam.DataValueField = "PaperID";
            ddl_exam.DataBind();

            BinderTorS(int.Parse(ddl_exam.SelectedValue));
        }


    }

    private void BinderForm(int id)
    {
        //VOTOAccessory voa = new VOTOAccessory();
        //TOAccessory obja = new TOAccessory();
        voClassExam = objClassExam.Load(id);

        ddl_CourseID.SelectedValue = voClassExam.Course_ID.ToString();
        ddl_exam.SelectedValue = voClassExam.Exam_ID.ToString();
        txt_Score.Text = voClassExam.Score.ToString();
        txt_ExamTime.Text = voClassExam.ExamTime.ToString();
    }

    private bool CheckPageData()
    {
        if (ViewState["CEID"] == null)
        {
            string search = string.Empty;
            if (ViewState["CourseID"] != null && ViewState["CID"] != null)
            {
                search = string.Format("Exam_ID = '{0}'", ddl_exam.SelectedValue);
                search += "and Course_ID=" + ViewState["CourseID"].ToString() + " and  ClassID=" + ViewState["CID"].ToString();
            }
            if (objClassExam.Count(search) != 0)
            {

                //lbl_msg_Error.Text = GetLanageValueByKey("lbl_msg_addjob_aspx_count", PowerCreator.LMS.Language.LanagetItemType.MSG);

                lbl_msg_Error.Text = "<br />该课程已经存在相同的试卷！"  ;
                return false;
            }
        }
        try 
        {
            int score = int.Parse(txt_Score.Text.Trim());
            int sec = int.Parse(txt_ExamTime.Text.Trim());
        }
        catch (Exception ex)
        {

            lbl_msg_Error.Text = "<br />考试时间与分数都必须填写，且必须为正整数！";
            return false;
        }
        return true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        
        if (ViewState["CEID"] == null)
        {
            voClassExam.ClassID = int.Parse(ViewState["CID"].ToString());
            voClassExam.Course_ID = int.Parse(ddl_CourseID.SelectedValue);
            voClassExam.Exam_ID = int.Parse(ddl_exam.SelectedValue);
            voClassExam.Score = int.Parse(txt_Score.Text.Trim());
            voClassExam.ExamTime = int.Parse(txt_ExamTime.Text.Trim());
            voClassExam.Createtime= DateTime.Now;
             PowerCreator.LMS.Web.Base.BasePage bpage=new PowerCreator.LMS.Web.Base.BasePage();
             voClassExam.Creator = bpage.GetCreatedByInfo().UserID;
             if (objClassExam.Add(voClassExam))
            {
                string url = "ManageClassExam.aspx?CID="  +ViewState["CID"].ToString();
                Script.RedirectURL(url);
            }
        }
        else
        {
            voClassExam = objClassExam.Load(int.Parse(ViewState["CEID"].ToString()));
            voClassExam.ClassID = int.Parse(ViewState["CID"].ToString());
            voClassExam.Course_ID = int.Parse(ddl_CourseID.SelectedValue);
            voClassExam.Exam_ID = int.Parse(ddl_exam.SelectedValue);
            voClassExam.Score = int.Parse(txt_Score.Text.Trim());
            voClassExam.ExamTime = int.Parse(txt_ExamTime.Text.Trim());
            voClassExam.Createtime = DateTime.Now;
            PowerCreator.LMS.Web.Base.BasePage bpage = new PowerCreator.LMS.Web.Base.BasePage();
            voClassExam.Creator = bpage.GetCreatedByInfo().UserID;
            if (objClassExam.Update(voClassExam))
            {
                string url = "ManageClassExam.aspx?CID=" + ViewState["CID"].ToString();
                Script.RedirectURL(url);
            }
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        string url = "ManageClassExam.aspx?CID=" + ViewState["CID"].ToString();
        Script.RedirectURL(url);
    }

    private void BinderTorS(int pid)
    {
        VOPaper vo = new VOPaper();
        vo = opPaper.LoadPaper(pid);
        txt_ExamTime.Text = vo.PaperTestTime.ToString();
        txt_Score.Text = vo.PaperScore.ToString();

    }

    protected void on_ddl_exam_change(object sender, EventArgs e)
    {
        int pid = int.Parse(ddl_exam.SelectedValue);
        BinderTorS(pid);

    }

}